package com.settlement.system.service.impl;

import com.settlement.system.model.entity.SysCalculateColumn;
import com.settlement.system.mapper.SysCalculateColumnMapper;
import com.settlement.system.service.SysCalculateColumnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysCalculateColumnQuery;
import com.settlement.system.model.form.SysCalculateColumnForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.SysCalculateColumnImportVo;

/**
 * <p>
 * 通过计算的字段配置 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-04-25
 */
@Service
public class SysCalculateColumnServiceImpl extends ServiceImpl<SysCalculateColumnMapper, SysCalculateColumn> implements SysCalculateColumnService {

    @Override
    public Page<SysCalculateColumn> getPage(SysCalculateColumnQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysCalculateColumn> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysCalculateColumn(SysCalculateColumnForm sysCalculateColumnForm) {
         SysCalculateColumn sysCalculateColumn = new SysCalculateColumn();
         BeanUtil.copyProperties(sysCalculateColumnForm, sysCalculateColumn);
         sysCalculateColumn.setCreateTime(null);
         return this.saveOrUpdate(sysCalculateColumn);
     }

     @Override
     public SysCalculateColumnForm getSysCalculateColumnFormData(Long id) {
         SysCalculateColumn sysCalculateColumn = this.getById(id);
         SysCalculateColumnForm sysCalculateColumnForm = new SysCalculateColumnForm();
         BeanUtil.copyProperties(sysCalculateColumn, sysCalculateColumnForm);
         return sysCalculateColumnForm;
     }

     @Override
     public boolean deleteSysCalculateColumn(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysCalculateColumnImportVo> voList)  {
        List<SysCalculateColumn> sysCalculateColumnList = new ArrayList<>();
        for (SysCalculateColumnImportVo importVo : voList) {
            SysCalculateColumn sysCalculateColumn = new SysCalculateColumn();
            BeanUtil.copyProperties(importVo, sysCalculateColumn);
            sysCalculateColumnList.add(sysCalculateColumn);
        }
        this.saveOrUpdateBatch(sysCalculateColumnList);
    }

    @Override
    public List<SysCalculateColumnImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysCalculateColumn> sysCalculateColumnList = this.getBaseMapper().selectBatchIds(ids);
        List<SysCalculateColumnImportVo> voList = new ArrayList<>();
        for (SysCalculateColumn sysCalculateColumn : sysCalculateColumnList) {
            SysCalculateColumnImportVo vo = new SysCalculateColumnImportVo();
            BeanUtil.copyProperties(sysCalculateColumn, vo);
            voList.add(vo);
        }
        return voList;
    }
}
